<?php
class ImageSliderWidget extends Widget {

    public $type;   //galleria
    public $theme;
    public $model;
    public $htmlOptions;
    
    private $_assetsUrl;
    
    /**
     * Initializes the widgets
     */
    public function init() {
        parent::init();
        if($this->_assetsUrl===null){
            $assetsDir=dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
            $this->_assetsUrl=Yii::app()->assetManager->publish($assetsDir);
        }
    }

    /**
     * Execute the widgets
     */
    public function run() {
        Yii::beginProfile(get_class($this));
        
        if(empty($this->model->gallery)) return;
        
        switch(strtolower($this->type)){
            case 'galleria': 
                $this->galleria();
                break;
        }//end switch
        
        Yii::endProfile(get_class($this));
    }

    private function galleria(){
        $script=Yii::app()->clientScript;
        $script->registerScriptFile($this->_assetsUrl . '/galleria/galleria-1.2.9.min.js', CClientScript::POS_HEAD)
               ->registerScriptFile($this->_assetsUrl . '/galleria/themes/classic/galleria.classic.min.js', CClientScript::POS_END)
               ->registerCss(__FUNCTION__, "
                    /* This rule is read by Galleria to define the gallery height: */
                    #galleria{height:320px}
                ");
        
        $this->htmlOptions['id']='galleria';
        switch($this->theme){
            case 'history':
            default:
                $script->registerScriptFile($this->_assetsUrl . '/galleria/plugins/history/galleria.history.min.js', CClientScript::POS_HEAD)
                    ->registerScript(__CLASS__, '
                        // Initialize Galleria
                        Galleria.run("#galleria");
                    ', CClientScript::POS_END)
                    ;
                
                echo CHtml::openTag('div',$this->htmlOptions);
                
                foreach($this->model->gallery as $image){
                    $item=CHtml::image($image->squareThumbnail, $image->caption, array_filter(array(
                        'data-title'=>$image->caption,
                        'data-description'=>$image->caption,
                    )));
                    echo CHtml::link($item, $image->mediumThumbnail);
                    
                }//end foreach
                
                echo CHtml::closeTag('div');
                break;
        }//end switch
    }
}//end class